package com.example.service.impl;

import com.example.entity.Department;
import com.example.entity.example.DepartmentExample;
import com.example.entity.bo.CourseStudentResultBO;
import com.example.mapper.DepartmentMapper;
import com.example.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired(required = false)
    DepartmentMapper departmentMapper;

    @Override
    public Department getById(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<Department> getByExampleWithLimit(DepartmentExample example, Integer page, Integer limit) {
        int begin = (page - 1) * limit;
        return departmentMapper.selectByExampleWithLimit(example, begin, limit);
    }

    @Override
    public List<CourseStudentResultBO> getCourseStudentResultByIdAndCourseId(String id, String courseId) {
        return departmentMapper.getCourseResultsByIdAndCourseId(id, courseId);
    }

    @Override
    public long getCountByExample(DepartmentExample example) {
        return departmentMapper.countByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    @Transactional
    public void updateById(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    @Transactional
    public void removeById(Integer id) {
        departmentMapper.deleteByPrimaryKey(id);
    }
}
